//
//  EventListController.m
//  whopaidforit
//
//  Created by Hoang Vu on 1/25/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "EventListController.h"

@interface EventListController ()

@end

@implementation EventListController

@synthesize tblItems, listData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}
- (void)viewWilAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CGRect newFrame = CGRectMake(0, 0, 320, 40);
    [self.navigationController.navigationBar setFrame:newFrame];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.listData = [[NSMutableArray alloc] init];
    [self changeStyle];
    [self createNavigation];
    [self loadData];
}

-(void) changeStyle
{
    [StyleControl background:self.view];
    self.tblItems.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tblItems.backgroundColor = [UIColor clearColor];
    self.tblItems.backgroundView = nil;
}
-(void) createNavigation
{
    UIButton *left =[UIButton buttonWithType:UIButtonTypeCustom];
    [left setBackgroundImage:[UIImage imageNamed:@"imageProfile"] forState:UIControlStateNormal];
    left.layer.cornerRadius = 5;
    left.layer.borderColor = [UIColor colorWithRed:0.278 green:0.275 blue:0.275 alpha:1.0].CGColor;
    left.layer.borderWidth = 1.0f;
    left.frame = CGRectMake(4, 4, 37, 37);
    Event *event = [Event instance];
    User *user = [User instance];
    self.listData = [event getDatasWS:user._id];
    
    UserClass *info = [[UserClass alloc] init];
    info = [user getAccountWS:user._id];
    if(![info.photo isEqualToString:@""] ){
        //-------load image in app locatio
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
        
        NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
        
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/AppImages"];
        
        NSString *fullPath = [[dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d",user._id]] stringByAppendingString:@".png"];
        
        UIImage *imageTemp =[UIImage imageWithContentsOfFile:fullPath];
        [left setBackgroundImage:imageTemp forState:UIControlStateNormal];
    }else if([info.avatar isEqualToString:@""] && info.facebookid)
    {
        NSString *urlString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture",info.facebookid];
        NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urlString]];
        UIImage *image = [[UIImage alloc] initWithData:imageData];
        [left setBackgroundImage:image forState:UIControlStateNormal];
    }
    NSString *title = [NSString stringWithFormat:NSLocalizedString(@"app.title", @"")];
    [StyleControl naviStyle:self.view andTitle:title andColor:1 andleftButton:left andRightButton:nil borderLeft:0 borderRight:0];
}

-(IBAction) openAccount:(id)sender
{
    AccountController *detail = [[AccountController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}

-(void) loadData
{
    Event *event = [Event instance];
    User *user = [User instance];
    self.listData = [event getDatasWS:user._id];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [listData count] + 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EventListCell *cell;
    
    if(indexPath.row == 0){
        cell = [[[EventListCell alloc] initWithStyle:UITableViewCellSeparatorStyleNone reuseIdentifier:@"Cell" status:0] autorelease];
        
    }else if(indexPath.row == [self.listData count]+1){
        cell = [[[EventListCell alloc] initWithStyle:UITableViewCellSeparatorStyleNone reuseIdentifier:@"Cell" status:2] autorelease];
    }else{
        cell = [[[EventListCell alloc] initWithStyle:UITableViewCellSeparatorStyleNone reuseIdentifier:@"Cell" status:1] autorelease];
        EventClass *event = [self.listData objectAtIndex:[indexPath row]-1];
        [cell.title setText:event.eventTitle];
    }
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
        //open event overview
        EventOverviewController *detail = [[EventOverviewController alloc] init];
        //[self.navigationController pushViewController:detail animated:YES];
        [self presentViewController:detail animated:YES completion:NULL];
        [detail release];
    }else if(indexPath.row == [self.listData count]+1){
        EventAddController *detail = [[EventAddController alloc] init];
        [self presentViewController:detail animated:YES completion:NULL];
        [detail release];
    }else{
        Event *event = [Event instance];
        EventClass *e = [listData objectAtIndex:indexPath.row-1];
        event._id = e.eventId;
        event._name = e.eventTitle;
        event.currencyId = e.currencyId;
        event.currency = e.currency;
        //active user
        if(e.active == 0){
            User *user = [User instance];
            [event activeEvent:user._id eventId:e.eventId];
        }
        BookingListController *detail = [[BookingListController alloc] init];
        [self presentViewController:detail animated:YES completion:NULL];
        [detail release];
    }
}
@end
